import Criteria from '@/utils/criteria'
import { OPERATOR } from '@/utils/constant'
import { getAction } from '@/api/manage'
export default {
	data() {
		return {
			queryParam: {},
			filters: {},
			sorts: [
				{property:'createTime',order:'desc'}
			],
			ipagination: {
				total: 0,
				currentPage: 1,
				pageSize: 10
			},
			dataSource:[],
			loadMoreLoading:'more',
			loading:false
		}
	},
	methods: {
		async loadData() {
			if (!this.urlObj.list) {
				return
			}
			let params = this.getQueryParams() // 查询条件
			this.loading = true
			if(this.showLoading){	
				uni.showLoading({
					 title: "加载中...",
					 mask: false
				});
			}
			this.loadMoreLoading = 'loading'
			let res = await getAction(this.urlObj.list, params)
			if(this.showLoading){	
				uni.hideLoading();
			}
			this.loading = false
			const data = res.data
			if(data.success && data.result.content.length>0){
				this.ipagination.total = data.result.totalElements
				this.dataSource = this.dataSource.concat(data.result.content)
			}
			if(this.ipagination.total <= this.dataSource.length){
				this.loadMoreLoading = 'noMore'
			}else{
				this.loadMoreLoading = 'more'
			}
		},
		getQueryParams() { // 获取查询条件
			const queryParam = new Criteria()
			queryParam.addCriterions(Object.assign({},this.filterObj(this.queryParam) , this.filters), OPERATOR.LIKE) // 过滤参数转换为jpa支持格式
			if (this.sorts.length > 0) {
				this.sorts.forEach(item => {
					queryParam.addOrder({ // 增加排序规则,支持组合排序
						fieldName: item.property,
						desc: item.order === 'desc'
					})
				})
			}
			var param = Object.assign({}, {
				criteria: queryParam.getEncode()
			})
			param.pageNo = this.ipagination.currentPage
			param.pageSize = this.ipagination.pageSize
			return param
		},
		filterObj(obj) {
		  if (!(typeof obj === 'object')) {
		    return
		  }
		
		  for (const key in obj) {
		    if (Object.prototype.hasOwnProperty.call(obj, key) && (obj[key] == null || obj[key] === undefined || obj[key] === '')) {
		      delete obj[key]
		    }
		  }
		  return obj
		},
		loadMore(){
			if(this.ipagination.currentPage < this.ipagination.total){	
				this.ipagination.currentPage++
				this.loadData()
			}
		},
		onSearch(){
			this.reset()
			this.loadData()
		},
		reloadData(){
			this.reset()
			this.loadData()
		},
		reset(){
			this.ipagination = {
				total: 0,
				currentPage: 1,
				pageSize: 10
			}
			this.dataSource = []
		}
	},
	created(){
		!this.disableMixinCreated && this.loadData()
	}
}